package cn.wolfcode.shop.cloud.web.controller;

import cn.wolfcode.shop.cloud.common.Result;
import cn.wolfcode.shop.cloud.redis.RedisService;
import cn.wolfcode.shop.cloud.service.ISeckillGoodService;
import cn.wolfcode.shop.cloud.util.SeckillKeyPrefix;
import cn.wolfcode.shop.cloud.vo.SeckillGoodVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("seckillGood")
public class SeckillGoodController {
    @Autowired
    private ISeckillGoodService seckillGoodService;

    @Autowired
    private RedisService redisService;

    @RequestMapping("initData")
    public String initData() {
        List<SeckillGoodVO> list = seckillGoodService.query();
        for (SeckillGoodVO seckillGoodVO : list) {
            redisService.set(SeckillKeyPrefix.SECKILL_GOOD_STOCK, seckillGoodVO.getId()+"", seckillGoodVO.getStockCount());
            redisService.hset(SeckillKeyPrefix.SECKILL_GOOD_HASH, "", seckillGoodVO.getId()+"", seckillGoodVO);
        }
        return "成功";
    }

    @RequestMapping("query")
    public Result query(){
        return new Result().success(seckillGoodService.queryByCache());
    }

    @RequestMapping("getSeckillGoodById")
    public Result getSeckillGoodById(Long seckillId){
        return new Result().success(seckillGoodService.findByCache(seckillId));
    }

}
